package dao

import (
	"cld-quick-cli/router/parameters"
	"cld-quick-cli/setup"
	"go.uber.org/zap"
)

// InsertSingleFile 插入文件信息到数据库！
func InsertSingleFile(file *parameters.SingleUploadFile) error {
	sql := `insert into files (file_id, user_id,upload_type,scene_type,base_url,
                   path,name,extension,size,year,month,day,upload_ip) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?);`
	_, err := setup.MysqlDB.Exec(sql, file.FileId, file.UserId, file.UploadType, file.SceneType, file.BaseUrl, file.Path, file.Name, file.Extension, file.Size, file.Year, file.Month, file.Day, file.UploadIp)
	if err != nil {
		zap.L().Error(err.Error())
		return err
	}
	return nil
}
func GetFilePathByFileId(fileId string) (string, error) {
	type PathMap struct {
		BaseUrl string `db:"base_url"`
		Path    string `db:"path"`
	}
	var pm PathMap
	sql := "select base_url,path from files where file_id = ?"
	err := setup.MysqlDB.Get(&pm, sql, fileId)
	if err != nil {
		zap.L().Error(err.Error())
		return "", err
	}
	pathUrl := pm.BaseUrl + pm.Path
	return pathUrl, nil
}
